/**
 * @param {number[][]} items1
 * @param {number[][]} items2
 * @return {number[][]}
 */
var mergeSimilarItems = function (items1, items2) {
  let len1 = items1.length,
    len2 = items2.length,
    arr = []
  let result = []
  for (let i = 0; i < len1; i++) {
    result[items1[i][0]] =result[items1[i][0]]?result[items1[i][0]]+ items1[i][1]:items1[i][1]
  }
  for (let i = 0; i < len2; i++) {
    result[items2[i][0]] += result[items2[i][0]]?result[items2[i][0]]+ items2[i][1]:items2[i][1]
  }

  for (let i = 0; i < result.length; i++) {
    if (result[i]) {
      arr.push([i, result[i]])
    }
  }
  return arr
}

mergeSimilarItems(
  [
    [1, 1],
    [4, 5],
    [3, 8],
  ],
  [
    [3, 1],
    [1, 5],
  ]
)
